﻿//************************************************************************************************************
//文件名称(File Name)：RolesMana.cs
//功能描述(Description)：角色管理
//数据表(Tables)：
//作者(Author)： 
//日期(Create Date)：20130214
//参考文档(Reference)(可选)：
//引用(Using) (可选)﹕
//************************************************************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DbAdapter.sqldbAdapter;

namespace AnalyseCrmMana
{
    public class RolesMana
    {
        public string roleno
        {
            get;
            set;
        }

        public string rolename
        {
            get;
            set;
        }

        public string roledesc
        {
            get;
            set;
        }

        public int AddNewRole(string roleno,string rolename,string roledesc)
        {
            string sql = "insert into Tbt_Roles(ROLENO,ROLENAME,ROLEDESC) values('" + roleno.Trim() + "','"
                        + rolename.Trim() + "','" + roledesc.Trim() + "')";
            ExecuteSql esql = new ExecuteSql();
            return esql.Execute_Sql(sql);
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="roleno">角色编号</param>
        /// <returns></returns>
        public int DeleteRole(string roleno)
        {
            string sql = "Delete from Tbt_Roles where ROLENO = '" + roleno.Trim() + "'";
            ExecuteSql esql = new ExecuteSql();
            return esql.Execute_Sql(sql);
        }
        /// <summary>
        /// 修改角色描述
        /// </summary>
        /// <param name="desc">角色描述</param>
        /// <returns></returns>
        public int UpdateRoleDesc(string roleno,string desc)
        {
            string sql = "Update Tbt_Roles set ROLEDESC = '" + desc + "'  where ROLENO = '" + roleno.Trim() + "'";
            ExecuteSql esql = new ExecuteSql();
            return esql.Execute_Sql(sql);
        }
        /// <summary>
        /// 角色关联用户
        /// </summary>
        /// <param name="roleno"></param>
        /// <param name="username"></param>
        /// <returns></returns>
        public int RelRoleAndUsers(string roleno, string username)
        {
            string sql = "Insert into Tbt_RoleAndUser(ROLENO,USERNAME) Values('" + roleno + "','" + username + "')";
            ExecuteSql esql = new ExecuteSql();
            return esql.Execute_Sql(sql);
        }
        /// <summary>
        /// 角色关联权限
        /// </summary>
        /// <param name="roleno"></param>
        /// <param name="authno"></param>
        /// <returns></returns>
        public int RelRoleAndAuthority(string roleno, int authno)
        {
            string sql = "Insert into Tbt_AuthorityAndRole(AUTHORITYNO,ROLENO) Values(" + authno + ",'" + roleno + "')";
            ExecuteSql esql = new ExecuteSql();
            return esql.Execute_Sql(sql);
        }
        /// <summary>
        /// 删除角色和权限的关联
        /// </summary>
        /// <param name="roleno"></param>
        /// <param name="authno"></param>
        /// <returns></returns>
        public int DelRoleAndAuthority(string roleno, int authno)
        {
            string sql = "delete from Tbt_AuthorityAndRole where AUTHORITYNO = " 
                    + authno + " and ROLENO = '" + roleno + "'";
            ExecuteSql esql = new ExecuteSql();
            return esql.Execute_Sql(sql);
        }

        /// <summary>
        /// 删除角色和用户的关联
        /// </summary>
        /// <param name="roleno"></param>
        /// <param name="authno"></param>
        /// <returns></returns>
        public int DelRoleAndUser(string roleno, string username)
        {
            string sql = "delete from Tbt_RoleAndUser where USERNAME = '"
                    + username + "' and ROLENO = '" + roleno + "'";
            ExecuteSql esql = new ExecuteSql();
            return esql.Execute_Sql(sql);
        }

        /// <summary>
        /// 获取所有角色
        /// </summary>
        /// <returns></returns>
        public IList<RolesMana> GetAllRoleList()
        {
            string sql = "select ROLENO,ROLENAME,ROLEDESC from Tbt_Roles";
            sqldb db = new sqldb();
            DataSet ds = db.ExecuteDataset(sqlconn.GetConnStr(), CommandType.Text, sql);
            IList<RolesMana> rolelist = null;
            if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
            {
                rolelist = new List<RolesMana>();
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    RolesMana rolemana = new RolesMana();
                    rolemana.roleno = dr["ROLENO"].ToString();
                    rolemana.rolename = dr["ROLENAME"].ToString();
                    rolemana.roledesc = dr["ROLEDESC"].ToString();
                    rolelist.Add(rolemana);
                }
            }
            return rolelist;
        }

        /// <summary>
        /// 角色数量
        /// </summary>
        /// <returns></returns>
        public int GetAllRoleListCount()
        {
            string sql = "select count(1) from Tbt_Roles";
            sqldb db = new sqldb();
            object rowcount = db.ExecuteScalar(sqlconn.GetConnStr(), CommandType.Text, sql);
            return Convert.ToInt32(rowcount);
        }
    }
}
